﻿using Common;
using IBLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DemoManageSys.Areas.Filters;
using Models.DTO;
using Microsoft.AspNetCore.Mvc;
using Models;

namespace DemoManageSys.Areas.Admin.Controllers
{
    [Area("Admin")]
    [MyFilter]
    public class OrderDetailController : Controller
    {
        private IOrderDetailBLL _orderDetailBLL;

        public OrderDetailController(
            IOrderDetailBLL orderDetailBLL
            )
        {
            _orderDetailBLL = orderDetailBLL;
        }

        #region 显示视图
        public IActionResult ListView()
        {
            return View();
        }

        public IActionResult CreateView()
        {
            return View();
        }

        public IActionResult EditView()
        {
            return View();
        }
        #endregion

        #region 获取订单的方法 (GetOrderDetailes)
        [HttpGet]
        public IActionResult GetOrderDetailH(int page, int limit)
        {
            int count;
            //调用BLL的查询方法
            List<GetShopInfoDTO> getOrderDetailDTOs = _orderDetailBLL.GetOrderDetailH(page, limit, out count);

            ReturnResule result = new ReturnResule()
            {
                Code = 0,
                Msg = "获取成功",
                Data = getOrderDetailDTOs,
                Count = count
            };

            return Json(result);
        }
        #endregion ()

        #region 添加的方法 (CreateOrderDetail)
        [HttpPost]
        public IActionResult CreateOrderDetail([FromForm] OrderDetail prod)
        {
            //实例化对象
            ReturnResule resule = new ReturnResule();
            string msg;//定义返回消息
            resule.IsSuccess = _orderDetailBLL.CreateOrderDetail(prod,out msg);//调用方法传值
            if (resule.IsSuccess)
            {
                resule.Code = 200;
                resule.Msg = msg;
            }
            else
            {
                resule.Msg = msg;

            }
            return Json(resule);
        }
        #endregion

        #region 删除的方法 (DeleteOrderDetail)
        /// <summary>
        /// 删除订单详情
        /// </summary>
        /// <param name="user">订单详情实体</param>
        /// <param name="msg">返回消息</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteOrderDetail(string id)
        {
            //实例化对象
            ReturnResule resule = new ReturnResule();
            if (string.IsNullOrWhiteSpace(id))
            {
                resule.Msg = "删除订单详情的id不能为空";
                return Json(resule);
            }

            resule.IsSuccess = _orderDetailBLL.DeleteOrderDetail(id);//调用方法传值
            if (resule.IsSuccess)
            {
                resule.Msg = "删除订单详情成功";
                resule.Code = 200;
            }
            else
            {
                resule.Msg = "删除订单详情失败";
            }
            return Json(resule);
        }
        #endregion

        #region 批量软删除订单详情的方法 (DeleteListOrderDetail)
        /// <summary>
        /// 批量删除订单详情
        /// </summary>
        /// <param name="ids">订单详情实体</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteListOrderDetail(List<string> ids)
        {
            //实例化对象
            ReturnResule resule = new ReturnResule();
            if (ids.Count == 0)
            {
                resule.Msg = "你没有返回任何订单详情";
                return Json(resule);
            }
            bool isOK = _orderDetailBLL.DeleteListOrderDetail(ids);//调用方法传值
            resule.Code = 200;
            resule.IsSuccess = isOK;
            resule.Msg = "删除订单详情成功";
            return Json(resule);
        }
        #endregion

        #region 根据ID获取下拉框 (GetOrderDetailById)
        [HttpGet]
        public IActionResult GetOrderDetailById(string id)
        {
            ReturnResule resule = new ReturnResule();

            if (string.IsNullOrWhiteSpace(id))
            {
                resule.Msg = "商品ID为空";
                return Json(resule);
            }

            OrderDetail prod = _orderDetailBLL.GetOrderDetailById(id);
            if(prod == null)
            {
                resule.Msg = "未获取到商品信息";
            }
            else
            {
                resule.Msg = "成功";
                resule.Code = 200;
                resule.Data = new {
                    prod = prod,//商品信息
                };
            }
            return Json(resule);
        }
        #endregion

    }
}